package com.misboot.document.controller;

import com.misboot.document.utils.BaseUtils;
import cn.ewsd.common.utils.StringUtils;
import com.misboot.document.utils.PageUtil.PageParam;
import com.misboot.document.utils.PageUtil.PageSet;
import com.misboot.document.model.Archive;
import com.misboot.document.model.Attachment;
import com.misboot.document.service.ArchiveService;
import com.misboot.document.service.AttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * File
 *
 * @Author 湖南佐佑时代科技有限公司
 * @Version 5.4.21
 * @Email service@zuoyo.com
 * @Date 2022-06-16 13:31:39
 */
@Controller
@RequestMapping("/archive")
public class ArchiveController extends DocumentBaseController {

    @Autowired
    private ArchiveService archivesService;

    @Autowired
    private AttachmentService attachmentService;

    /**
     * @MethodName 获取数据分页集
     * @Description
     * @Param pageParam 
     * @Param categoryId 
     * @Return java.lang.Object
     * @Date 2020/12/10 9:40 上午
     */
    @ResponseBody
    @RequestMapping(value = "getPageSetData")
    public Object getPageSetData(PageParam pageParam, String categoryId) throws Exception {
        String filterSort = "";
        if (!StringUtils.isNullOrEmpty(categoryId)) {
            Map map = new HashMap();
            map.put("p1", "sys_channel");
            map.put("p2", categoryId);
            map.put("p3", "idStr");
            archivesService.getChildIds(map);
            categoryId = map.get("p3").toString();
            filterSort = " AND category_id in (" + categoryId + " )";
        }
        filterSort = BaseUtils.filterSort(request, filterSort);
        PageSet<Archive> pageSet = archivesService.getPageSet(pageParam, filterSort);
        return pageSet;
    }

    /**
     * @MethodName 根据uuid获取详情
     * @Description
     * @Param  
     * @Return java.lang.Object
     * @Date 2020/12/10 9:40 上午
     */
    @ResponseBody
    @RequestMapping("getDetailByUuid")
    public Object getDetailByUuid() throws Exception {
        String uuid = request.getParameter("uuid");
        Archive info = archivesService.selectByPrimaryKey(uuid);
        return info;
    }


    /**
     * @MethodName 保存
     * @Description
     * @Param resource 
     * @Return java.lang.Integer
     * @Date 2020/12/10 9:41 上午
     */
    @ResponseBody
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public Integer save(Archive resource) throws Exception {
        return archivesService.insertSelective(getSaveData(resource));
    }

    /**
     * @MethodName 根据uuid获取详情数据返回map
     * @Description
     * @Param uuid 
     * @Return java.lang.Object
     * @Author 朱永敬<zhuyongjing@zuoyour.com>
     * @Date 2020/12/10 9:41 上午
     */
    @ResponseBody
    @RequestMapping("detail")
    public Object detail(String uuid) throws Exception {
        Archive archive = archivesService.selectByPrimaryKey(uuid);
        List<Attachment> attachments = attachmentService.getListByPuuid(archive.getUuid());
        Map<String, Object> map = new HashMap<>();
        map.put("archive", archive);
        map.put("attachments", attachments);
        return map;
    }

    /**
     * @MethodName 修改
     * @Description
     * @Param archive 
     * @Return java.lang.Integer
     * @Date 2020/12/10 9:41 上午
     */
    @ResponseBody
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public Integer update(Archive archive) throws Exception {
        Archive oriData = archivesService.selectByPrimaryKey(archive.getUuid());
        archive.setCreatorId(oriData.getCreatorId());
        archive.setCreator(oriData.getCreator());
        archive.setCreateTime(oriData.getCreateTime());
        archive.setModifierId(getCurrentUserNameId());
        archive.setModifier(getCurrentUserName());
        archive.setModifyTime(new Date());
        List<Attachment> attachments = attachmentService.getListByUuid(archive.getUuid());
        return archivesService.updateByPrimaryKeySelective(archive);
    }

    /**
     * @MethodName 删除
     * @Description
     * @Param  
     * @Return java.lang.Object
     * @Author 朱永敬<zhuyongjing@zuoyour.com>
     * @Date 2020/12/10 9:42 上午
     */
    @ResponseBody
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public Object delete() throws Exception {
        Integer result = 0;
        String uuid = request.getParameter("uuid");
        String[] uuids = uuid.split(",");
        for (int i = 0; i < uuids.length; i++) {
            result = archivesService.deleteByPrimaryKey(uuids[i]);
            result++;
        }
        return result > 0 ? success("删除成功") : failure("删除失败");
    }


}
